import javaService from '@/Yapi/java/javaHttp'

/**
 * 字典管理API
 */

// 获取全部字典
export function getAllDict() {
  return javaService({
    url: `${process.env.VUE_APP_DEV_JAVA_BASE_HOST}/dict/getAll`,
    method: 'get'
  })
}

// 添加字典
export function addDict(data) {
  return javaService({
    url: `${process.env.VUE_APP_DEV_JAVA_BASE_HOST}/dict/add`,
    method: 'post',
    data,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    transformRequest: [function(data) {
      let ret = ''
      for (const it in data) {
        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
      }
      return ret.slice(0, -1)
    }]
  })
}

// 编辑字典
export function editDict(data) {
  return javaService({
    url: `${process.env.VUE_APP_DEV_JAVA_BASE_HOST}/dict/edit`,
    method: 'post',
    data,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    transformRequest: [function(data) {
      let ret = ''
      for (const it in data) {
        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
      }
      return ret.slice(0, -1)
    }]
  })
}

// 删除字典
export function deleteDict(id) {
  return javaService({
    url: `${process.env.VUE_APP_DEV_JAVA_BASE_HOST}/dict/delByIds/${id}`,
    method: 'delete'
  })
}

// 搜索字典
export function searchDict(key) {
  return javaService({
    url: `${process.env.VUE_APP_DEV_JAVA_BASE_HOST}/dict/search`,
    method: 'get',
    params: { key }
  })
}
